package com.xiaolun.nio;

import java.nio.ByteBuffer;

// 转成只读buffer
public class ReadOnlyBuffer {
	public static void main(String[] args) {
		//创建一个buffer
		ByteBuffer buffer = ByteBuffer.allocate(64);

		// 放入64个字节数据
		for (int i = 0; i < 64; i++) {
			buffer.put((byte) i);
		}
		//读取
		buffer.flip();
		//得到一个只读的Buffer-asReadOnlyBuffer
		ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
		//class java.nio.HeapByteBufferR
		System.out.println(readOnlyBuffer.getClass());

		//读取
		while (readOnlyBuffer.hasRemaining()) {
			System.out.println(readOnlyBuffer.get());
		}
		//写入会有报出异常：ReadOnlyBufferException
		readOnlyBuffer.put((byte) 100);
	}
}
